package com.ziq.zcamp.bean.post

import android.os.Parcel
import android.os.Parcelable
import com.luck.picture.lib.entity.LocalMedia
import com.ziq.zcamp.util.PictureUtil

class PostMedia() : Parcelable {
    var networkInfo:NetworkInfo? = null
    var localMedia:LocalMedia? = null

    var id:Int = 0
        private set
        get() {
            if(localMedia != null){
                return localMedia.hashCode()
            }
            if(networkInfo != null){
                return networkInfo.hashCode()
            }
            return 0
        }

    fun getUrl():String{
        var result = ""
        if(localMedia != null){
            result = "file://" + PictureUtil.getPath(localMedia!!)
        } else if(networkInfo != null){
            result = networkInfo?.url?:""
        }
        return result
    }

    fun getWidth():Int{
        var result = 0
        if(localMedia != null){
            result = localMedia!!.width
        } else if(networkInfo != null){
            result = networkInfo?.width?:0
        }
        return result
    }

    fun getHeight():Int{
        var result = 0
        if(localMedia != null){
            result = localMedia!!.height
        } else if(networkInfo != null){
            result = networkInfo?.height?:0
        }
        return result
    }

    constructor(parcel: Parcel) : this() {
        networkInfo = parcel.readParcelable(NetworkInfo::class.java.classLoader)
        localMedia = parcel.readParcelable(LocalMedia::class.java.classLoader)
    }


    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeParcelable(networkInfo, flags)
        parcel.writeParcelable(localMedia, flags)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<PostMedia> {
        override fun createFromParcel(parcel: Parcel): PostMedia {
            return PostMedia(parcel)
        }

        override fun newArray(size: Int): Array<PostMedia?> {
            return arrayOfNulls(size)
        }
    }

    class NetworkInfo() : Parcelable{
        var url :String? = null
        var width :Int? = null
        var height :Int? = null
        var water : String ?= null

        constructor(parcel: Parcel) : this() {
            url = parcel.readString()
            water = parcel.readString()
            width = parcel.readValue(Int::class.java.classLoader) as? Int
            height = parcel.readValue(Int::class.java.classLoader) as? Int
        }

        override fun writeToParcel(parcel: Parcel, flags: Int) {
            parcel.writeString(url)
            parcel.writeString(water)
            parcel.writeValue(width)
            parcel.writeValue(height)
        }

        override fun describeContents(): Int {
            return 0
        }

        companion object CREATOR : Parcelable.Creator<NetworkInfo> {
            override fun createFromParcel(parcel: Parcel): NetworkInfo {
                return NetworkInfo(parcel)
            }

            override fun newArray(size: Int): Array<NetworkInfo?> {
                return arrayOfNulls(size)
            }
        }
    }
}